package com.example.wc.logindemo;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginDemoActivity extends AppCompatActivity{

    private EditText mUser;
    private EditText mPassword;
    private TextView mLoginBtn;
    private TextView mRegisterTextviewLogin;//还没有帐号？立即注册

    public static String ACCESS = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.CustomTitleBarTheme);
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_login_demo);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.login_title_bar);

        mUser = (EditText)findViewById(R.id.user_edittext_login);
        mPassword = (EditText)findViewById(R.id.password_edittext_login);
        mLoginBtn = (TextView)findViewById(R.id.login_button_login);
        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
                volleyRequestDemo.volleyRequestDemo_POST_Login(LoginDemoActivity.this, new VolleyRequestDemo.BlogPostListener() {
                    @Override
                    public void onBlogPosted() {
                        JSONObject data = null;
                        try {
                            data = volleyRequestDemo.getResponse().getJSONObject("data");
                            String mobile = data.getString("mobile");
                            String accessToken = data.getString("accessToken");
                            ACCESS = accessToken;

                            Intent i = new Intent(LoginDemoActivity.this, BlogsListActivity.class);
                            startActivity(i);
                            Toast.makeText(LoginDemoActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

        mRegisterTextviewLogin=(TextView)this.findViewById(R.id.register_textview_login);
        setForegroundColorSpan();
        mRegisterTextviewLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(LoginDemoActivity.this, RegisterDemoRegister.class);
                startActivity(i);
            }
        });

    }

    //登录页面-立即注册自定义颜色
    private void setForegroundColorSpan() {
        SpannableString spanString = new SpannableString(this.getString(R.string.register_login));
        ForegroundColorSpan span = new ForegroundColorSpan(Color.rgb(255,85,93));
        spanString.setSpan(span, 6, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mRegisterTextviewLogin.setText(spanString);
    }

}
